﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using CLEXA.AdminClient.View;
using CLEXA.AdminClient.ViewModel;

namespace CLEXA.AdminClient
{
    internal class WindowHelper : IWindowHelper
    {
        private readonly Dictionary<VMBase, Window> viewModels = new Dictionary<VMBase, Window>();
        private readonly Dictionary<Window, VMBase> views = new Dictionary<Window, VMBase>();
        private readonly object sync = new object();

        public void ShowCreateUser(IFacade facade)
        {
            UserCreateVM vm = new UserCreateVM(facade);
            UserCreateView vw = new UserCreateView();
            vw.DataContext = vm;
            lock (sync)
            {
                views.Add(vw, vm);
                viewModels.Add(vm, vw);
                vw.Closed += VW_Closed;
            }
            vw.ShowDialog();
        }

        public void ShowUpdateUser(IFacade facade, int userId)
        {
            UserEditVM vm = new UserEditVM(facade, userId);
            UserEditView vw = new UserEditView();
            vw.DataContext = vm;
            lock (sync)
            {
                views.Add(vw, vm);
                viewModels.Add(vm, vw);
                vw.Closed += VW_Closed;
            }
            vw.ShowDialog();
        }

        public void ShowUserChangePassword(IFacade facade, int userId)
        {
            UserChangePasswordVM vm = new UserChangePasswordVM(facade, userId);
            UserChangePasswordView vw = new UserChangePasswordView();
            vw.DataContext = vm;
            lock (views)
            {
                views.Add(vw, vm);
                viewModels.Add(vm, vw);
                vw.Closed += VW_Closed;
            }
            vw.ShowDialog();
        }

        private void VW_Closed(object sender, EventArgs e)
        {
            lock (sync)
            {
                Window vw = (Window)sender;
                VMBase vm;
                if (views.TryGetValue(vw, out vm))
                {
                    views.Remove(vw);
                    viewModels.Remove(vm);
                }
                vw.Closed -= VW_Closed;
            }
        }

        public void CloseWindow(VMBase vm)
        {
            lock (sync)
            {
                Window vw;
                if (viewModels.TryGetValue(vm, out vw))
                {
                    views.Remove(vw);
                    viewModels.Remove(vm);
                }
                vw.Closed -= VW_Closed;
                vw.Close();
            }
        }

        public void ShowError(string message, Exception innerException)
        {
            MessageBox.Show(string.Format("{0}\n\n details: {1}", message, innerException), "CLEXA - Admin", MessageBoxButton.OK);
        }

        public bool AskYesOrNo(string question)
        {
            return MessageBox.Show(question, "CLEXA - Admin", MessageBoxButton.YesNo) == MessageBoxResult.Yes;
        }
    }
}
